package weaver.formmode.customjavacode.modeexpand.holyview.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MyMD5 {
    private final String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e",
            "f" };

    public String encodeByMD5(String strMD5) {
        if (strMD5 != null) {
            try {
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                byte[] results = messageDigest.digest(strMD5.getBytes());
                StringBuffer strb = new StringBuffer();
                for (int i = 0; i < results.length; i++) {
                    strb.append(byteToHexString(results[i]));
                }
                String strMD = strb.toString();
                return strMD.toUpperCase();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n += 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return this.hexDigits[d1] + this.hexDigits[d2];
    }

    public String createPassWord(String inputString) {
        return encodeByMD5(inputString);
    }

    public static void main(String[] args) {
        MyMD5 myMD5 = new MyMD5();
        String password = myMD5.createPassWord("1");
        String AA = "c4ca4238a0b923820dcc509a6f75849b";

        System.out.println("PASSWORD: " + password);
    }
}
